package com.nut.lmsc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.lmsc.api.req.GoodsQueryReq;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.*;
import com.nut.lmsc.enums.GoodsStatusEnum;
import com.nut.lmsc.mapper.*;
import com.nut.lmsc.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Autowired
    private GoodsSpecMapper goodsSpecMapper;

    @Autowired
    private GoodsSellModeMapper goodsSellModeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private CouponWhiteMapper couponWhiteMapper;

    @Override
    public Result<IPage<Goods>> listPage(GoodsQueryReq req) {
        IPage<Goods> list = goodsMapper.listPage(req.newPage(), req.getLon(),
                req.getLat(), req.getDistance(), req.getSellMode(), req.getCatPathId(), req.getShopId(), req.getStatus(), req.getName());
        return Result.ok(list);
    }

    @Override
    public Result<Goods> getById(Integer id) {

        Goods goods = goodsMapper.selectById(id);

        GoodsDetail detail = goodsDetailMapper.selectById(id);
        detail.filled(goods);

        List<GoodsSpec> specList = goodsSpecMapper.selectByGoodsId(id);
        goods.setSpecList(specList);

        List<GoodsSellMode> modeList = goodsSellModeMapper.selectByGoodsId(id);
        goods.setModeList(modeList);

        return Result.ok(goods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result add(Goods goods) {

        Integer min = minPrice(goods.getSpecList());
        goods.setPrice(min);

        Integer shopId = goods.getShopId();
        if (shopId == null) {
            return Result.fail("shopId==null");
        }

        Shop shop = shopMapper.selectById(shopId);
        if (shop == null) {
            return Result.fail("找不到店铺");
        }

        goods.setShopName(shop.getName());
        goods.setStatus(GoodsStatusEnum.INIT.getValue());

        goodsMapper.insert(goods);

        GoodsDetail detail = goods.getGoodsDetail();
        detail.setId(goods.getId());
        goodsDetailMapper.insert(detail);

        List<GoodsSpec> specList = goods.getSpecList();
        if (specList != null) {
            for (GoodsSpec goodsSpec : specList) {
                goodsSpec.setGoodsId(goods.getId());
                goodsSpecMapper.insert(goodsSpec);
            }
        }
        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateById(Goods goods) {

        Integer min = minPrice(goods.getSpecList());
        goods.setPrice(min);

        //不能更新
        goods.setShopName(null);
        goods.setShopId(null);

        if (goods.getStatus() != null) {

            //商品上架
            if (goods.getStatus().equals(GoodsStatusEnum.ON.getValue())) {

                Goods goods1 = goodsMapper.selectById(goods.getId());

                Shop shop = shopMapper.selectById(goods1.getShopId());
                if (shop == null) {
                    return Result.fail("找不到店铺");
                }

                if (shop.getGuarantee().equals(0)) {
                    return Result.fail("请先缴纳保证金，上架商品");
                }

                if (goods.getCouponRatio() == null) {
                    return Result.fail("请输入代金券比例");
                }

                if (goods.getCouponRatio() < 20) {
                    //不在白名单内
                    CouponWhite white = couponWhiteMapper.selectByMobile(shop.getMobile());
                    if (white == null) {
                        return Result.fail("当前用户保证金不能低于20%");
                    }
                }

            }

        }


        if (goods.needUpdate()) {
            goodsMapper.updateById(goods);
        }

        if (goods.needUpdateDetail()) {
            GoodsDetail detail = goods.getGoodsDetail();
            goodsDetailMapper.updateById(detail);
        }

        List<GoodsSpec> specList = goods.getSpecList();
        if (specList != null) {
            for (GoodsSpec goodsSpec : specList) {
                Integer id = goodsSpec.getId();
                if (id == null) {
                    goodsSpec.setGoodsId(goods.getId());
                    goodsSpecMapper.insert(goodsSpec);
                } else {
                    goodsSpecMapper.updateById(goodsSpec);
                }
            }
        }

        return Result.ok();
    }

    private Integer minPrice(List<GoodsSpec> list) {
        if (list == null) {
            return null;
        }
        Integer min = null;
        for (GoodsSpec goodsSpec : list) {
            if (min == null) {
                min = goodsSpec.getPrice();
                continue;
            }
            if (min > goodsSpec.getPrice()) {
                min = goodsSpec.getPrice();
            }
        }
        return min;
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            goodsMapper.deleteById(id);
        }
        return Result.ok();
    }
}